<?php
class Regions extends MY_Controller
{
	var $curl = 'regions';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
			
	function __construct()
	{
		parent::__construct();
				
		$this->load->model(array('/public/geo_city_model', '/public/geo_region_model', '/public/geo_state_model', '/public/geo_country_model'));

		$this->validation->set_fields($this->geo_region_model->fields);

		$this->data['Title'] = T_('Районы');

		$this->language = parent::getLang();

		$this->name = T_('Районы');
	}
	
	function index()
	{
		$this->geo_region_model->search();

		$this->renderLayout();
	}

	function create()
	{
		$this->data['Title'] = $this->name . ' / ' . T_('Create');

		$this->validation->id_countries = ($this->input->post('id_countries')) ? $this->input->post('id_countries') : 0;
		$this->validation->id_states = $this->input->post('id_states');
		
		if (count($_POST))
		{	
			$rules = array('id_countries', 'id_states', 'name');
			$this->validation->set_rules($this->geo_region_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{
				$item['id_states'] 	= $this->validation->id_states;
				$item['name'] 		= $this->validation->name;				
				
				$this->geo_region_model->insert($item);

				redirect($this->curl . '/index');
			}
		}

		$this->renderLayout();		
	}
		
	function edit($id = null)
	{
		if (!empty($id))
		{
			$object = $this->geo_region_model->get($id);
			$this->data['Title'] = $object['name'] . ' / ' . T_('Редактировать');
					
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 		 = $object['id'];
				$this->validation->id_countries = $object['id_countries'];
				$this->validation->id_states = $object['id_states'];  
				$this->validation->name 	 = $object['name'];
			}
			else
			{
				$rules = array('id_countries', 'id_states', 'name');
				$this->validation->set_rules($this->geo_region_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id']		   = $this->validation->id;
					$item['id_states'] = $this->validation->id_states;
					$item['name'] 	   = $this->validation->name;				
											
					$this->geo_region_model->update($item);
					
					redirect($this->curl . '/index');				
				}
			}			
		}
		
		$this->renderLayout();
	}

	function deleteItems()
	{
		$data = $this->geo_region_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id)
	{
		$data = $this->geo_region_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index');	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}		
	}
	
	function loadRegionsByStateId($id_states = null, $wrap_elem = '')
	{
		$this->layout = 'ajax';
		$this->data['id_states'] = $id_states;
		$this->data['wrap_elem']  = $wrap_elem;
		$onchange = '';
		
		if ($wrap_elem)
		{
			$onchange = 'onchange="loadCities(this.value)"';
		}

		$this->data['onchange']  = $onchange;
		$this->load->view('regions/ajax/select_regions', $this->data);
	}

	function fillRegions()
	{
		$sql = 'SELECT * FROM "cities" WHERE "lang_id"=2 ORDER BY "city_state_ID", "city_declension", "city_name"';
		$regions = $this->db->query($sql)->result();		

		$region_name = '';
		foreach($regions as $region)
		{
			if ($region->city_declension)
			{
				if ($region_name != $region->city_declension)
				{
					$item['id_states'] = $region->city_state_ID;
					$item['name'] = $region->city_declension;
					$this->geo_region_model->insert($item);
					
					$id_region = $this->db->insert_id();						
				}
				
				$region_name = $region->city_declension;
			}
			else
			{
				//echo $region->city_name."<br>";
				$id_region = null;
			}

			$item2['id_states'] = $region->city_state_ID;
			$item2['id_regions'] = $id_region;
			$item2['name'] = (!empty($region->city_name)) ? $region->city_name : '';			
			$this->geo_city_model->insert($item2);
		}
	}
	
/*
	function loadCitiesByRegionId($region_ID = null, $wrap_elem = '')
	{
		$this->layout = 'ajax';
		$this->data['region_ID'] = $region_ID;
		$this->data['wrap_elem']  = $wrap_elem;
		$onchange = '';
		
		if ($wrap_elem == 'place_cities_2')
		{
			//$onchange = 'onchange="loadHotels(this.value, \'place_hotels_2\')"';
		}
		else if ($wrap_elem == 'place_cities')
		{
			$onchange = 'onchange="loadHotels(this.value, \'place_hotels\')"';
		}
		$this->data['onchange']  = $onchange;
		$this->load->view('geo/ajax/select_cities', $this->data);
	}	
	*/
}
?>